﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using STX.Repository;
using STX.Service;

namespace STX.Admin.Export
{
    public partial class PayInfoDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (DateTime.Now.Hour < 8)
                {
                    this.ReportViewer1.ShowPrintButton = false;
                }
                else
                {
                    this.ReportViewer1.ShowPrintButton = true;
                }

                for (DateTime dt = DateTime.Now; dt >= DateTime.Now.AddYears(-10); dt = dt.AddYears(-1))
                {
                    this.DropDownList1.Items.Add(new ListItem(dt.Year.ToString(), dt.Year.ToString()));
                }
                for (int i = 1; i <= 12; i++)
                {
                    this.DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                GenerateReport(DateTime.Now.Year,DateTime.Now.Month);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            
        }

        private void GenerateReport(int year,int month)
        {

            this.ReportViewer1.LocalReport.ReportPath
                = Server.MapPath("~/Administrator/Export/ReportViewerTemplates/PayInfosDetail.rdlc");

            BizPayInfoService BizPayInfoService = new BizPayInfoService();
            BizPayInfoService.Transaction = new EfTransactionContext();

            var dataSource
                = new ReportDataSource("DataSet1", BizPayInfoService.GetPayInfoDetail());

            DateTime dt = new DateTime(year, month, 1);
            ReportParameter rp = new ReportParameter("starttime", (dt).ToString("yyyy年MM月dd日"));
            ReportParameter rp1 = new ReportParameter("endtime", (dt.LastDayOfMonth()).ToString("yyyy年MM月dd日"));

            //this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp,rp1 });

            this.ReportViewer1.LocalReport.DataSources.Clear();
            this.ReportViewer1.LocalReport.DataSources.Add(dataSource);
        }

        protected void btnGenerate(object sender, EventArgs e)
        {
            int year = DateTime.Now.Year;
            int.TryParse(this.DropDownList1.SelectedValue, out year);

            int month = DateTime.Now.Month;
            int.TryParse(this.DropDownList2.SelectedValue, out month);

            GenerateReport(year,month);
        }
    }
}